package com.my.study;

import com.my.study.bean.User;
import com.my.study.config.MultiTxConfig;
import com.my.study.jdbc.entity.Account;
import com.my.study.jdbc.entity.BookEntity;
import com.my.study.jdbc.service.AccountService;
import com.my.study.jdbc.service.BookService;
import com.my.study.jdbc.service.MultiService;
import com.my.study.jdbc.service.UserService;
import com.my.study.utils.SpringContextUtil;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/11/3 23:13
 **/
public class Spring5Test {

    @Test
    public void testUserAdd() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        User user = context.getBean("user", User.class);
        user.add();

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        Resource[] resources = resolver.getResources("bean.xml");

        System.out.println(resources);
    }


    @Test
    public void jdbcTest() {
        ApplicationContext context = new ClassPathXmlApplicationContext("jdbc.xml");
        AccountService accountService = context.getBean(AccountService.class);

        // 新增
        Account insertAccount = new Account();
        insertAccount.setUserName("jack");
        insertAccount.setMoney(1200);
        accountService.add(insertAccount);

        // 修改
//        Account account = new Account(1L, "ydj", 1223);
//        int update = bookService.update(account);
//        System.out.println(update);
    }


    @Test
    public void accountTest() {
        ApplicationContext context = new ClassPathXmlApplicationContext("jdbc.xml");
        UserService userService = context.getBean(UserService.class);
        userService.accountMoney(100, "lucy", "mary");
    }

    @Test
    public void accountAnnoTest() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MultiTxConfig.class);
        UserService userService = context.getBean(UserService.class);
        userService.accountMoney(100, "lucy", "mary");
    }

    @Test
    public void bookAnnoTest() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MultiTxConfig.class);
        BookService bookService = context.getBean(BookService.class);

        BookEntity book = new BookEntity();
        book.setBookId(1L);
        book.setAuthor("大神");

        bookService.update(book);
    }

    @Test
    public void bookAnnoWithTxTest() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MultiTxConfig.class, SpringContextUtil.class);
        BookService bookService = context.getBean(BookService.class);

        BookService bookService1 = SpringContextUtil.getBean(BookService.class);

        System.out.println(bookService == bookService1);

//        BookEntity bookOne = new BookEntity();
//        bookOne.setBookId(1L);
//        bookOne.setAuthor("张三1");
//
//        BookEntity bookTwo = new BookEntity();
//        bookTwo.setBookId(2L);
//        bookTwo.setAuthor("王麻子2");
//
//        bookService.multiUpdate(bookOne, bookTwo);
    }

    @Test
    public void multiTxTest() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MultiTxConfig.class, SpringContextUtil.class);

        MultiService multiService = context.getBean(MultiService.class);

        Account account = new Account();
        account.setId(1L);
        account.setMoney(900);

        BookEntity book = new BookEntity();
        book.setBookId(2L);
        book.setAuthor("王麻子");

        multiService.multiUpdate(account, book);

    }
}
